<!--
 * @Descripttion: 客户管理列表页
 * @version:
 * @Author: cxguo
 * @Date: 2019-08-02 15:03:05
 * @LastEditors: cxguo
 * @LastEditTime: 2019-11-21 10:46:27
 -->
<template>
  <div class="app-container paddingT50">
    <affix>
      <div class="paddingLR20 clearfix">
        <el-card shadow="never" class="oper-btn-card">
          <div class="clearfix">
            <el-form
              class="fl form-no-bottom"
              :inline="true"
              :model="searchData"
              label-position="left"
              label-width="80px"
              @keydown.enter.native="btnSearch"
            >
              <el-form-item label="所属门店: ">
                <el-select
                  v-model="searchData.shopId"
                  clearable
                  filterable
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in optionsData.shop"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-input
                  v-model="searchData.keywords"
                  clearable
                  style="width: 360px"
                  placeholder="按编号,名称,联系人,电话..."
                >
                  <el-button slot="append" icon="el-icon-search" type="primary" :loading="btnLoading.search" @click="btnSearch()" />
                </el-input>
              </el-form-item>
            </el-form>
            <el-button class="fr" type="primary" @click="btnAddStore">新增仓库</el-button>
          </div>
        </el-card>
      </div>
    </affix>
    <div>
      <vxe-grid
        ref="table"
        class="grid"
        border
        size="small"
        resizable
        highlight-hover-row
        :loading="tableLoading.store"
        :edit-config="{trigger: 'click', mode: 'row'}"
        :columns="columnsData"
        :data="tableData.store"
        @edit-closed="onEditClosed"
      />
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="pageInfo.rows"
        :page-sizes="[20,30,40]"
        :current-page.sync="pageInfo.page"
        :total="pageInfo.totalCount"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />
    </div>
    <el-dialog
      v-if="dialogVisible"
      title="新增仓库"
      :visible.sync="dialogVisible"
      append-to-body
    >
      <save-store ref="saveStore" :operation="storeInfo.oper" :data="storeInfo.data" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="btnLoading.sure" @click="btnSureSaveData">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { oftenuse } from '@/libs/mixins'
import { listData, addData, updateData, delListData } from '@/api/store/store.js'
import { getOptions } from '@/api/store/shop.js'
import Affix from '@/components/Affix'
import SaveStore from './save-store'
import ViewStore from './view-store'
import { getDelButton, getUpdateButton } from '@/components/Table/oper-btns.js'

export default {
  name: 'StoreList',
  filters: {
  },
  // eslint-disable-next-line vue/no-unused-components
  components: { Affix, SaveStore, ViewStore },
  mixins: [oftenuse],
  data() {
    return {
      searchData: {},
      storeInfo: {
        oper: 'add',
        data: {}
      },
      dialogVisible: false,
      optionsData: {},
      columnsData: [
        { title: '#', width: 30, type: 'expand',
          slots: {
            default: ({ row, seq }) => {
              const data = {
                props: {
                  data: row
                }
              }
              return [<ViewStore {...data}/>]
            }
          }
        },
        { title: '操作', width: 80,
          slots: {
            default: (params) => {
              const { row } = params
              const h = this.$createElement
              const delBtnNode = getDelButton(h, { on: { click: () => { this.btnDelData(row) } }})
              const updateBtnNode = getUpdateButton(h, { props: { type: 'primary' },
                on: { click: () => { this.btnUpdateData(row) } }})
              const divNode = h('div', {
                attrs: {
                  class: 'table-oper-btns'
                }
              }, [updateBtnNode, delBtnNode])
              return [divNode]
            }
          }
        },
        { field: 'code', title: '编号', editRender: { name: 'ElInput' }, width: 150 },
        { field: 'name', title: '名称', editRender: { name: 'ElInput' }},
        { field: 'shopName', title: '所属门店' },
        { field: 'contacter', title: '联系人', editRender: { name: 'ElInput' }},
        { field: 'phone', title: '联系电话', editRender: { name: 'ElInput' }},
        { field: 'status', title: '状态', width: 80,
          cellRender: {
            name: 'ElSwitch',
            props: { activeValue: '1', inactiveValue: '0' },
            events: {
              change: this.onSwitchStatusChange
            }
          }
        }
      ]
    }
  },
  watch: {
  },
  created() {
    this.initOptionsData()
    this.getStoreList()
  },
  methods: {
    btnAddStore() {
      this.storeInfo.oper = 'add'
      this.dialogVisible = true
    },
    btnSureSaveData() {
      const storeComp = this.$refs.saveStore
      storeComp.valiData().then(_ => {
        const data = storeComp.getData()
        this.setBtnLoading('sure', true)
        this.addStore(data).then(res => {
          this.setBtnLoading('sure', false)
        })
      })
    },
    btnSearch() {
      this.setBtnLoading('search', true)
      this.getStoreList().then(res => {
        this.setBtnLoading('search', false)
      }).catch(err => {
        console.log(err)
        this.setBtnLoading('search', false)
      })
    },
    // table btns
    btnUpdateData(row) {
      this.storeInfo.oper = 'update'
      this.storeInfo.data = row
      this.dialogVisible = true
    },
    btnDelData(row) {
      this.$confirm('确定要删除该仓库吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delListStore([row.id])
      }).catch(() => {
      })
    },
    // api
    delListStore(idList) {
      delListData(idList).then(res => {
        const message = res.data.message
        if (!res.data.flag) return this.$notify.error({ title: '删除失败!', message: message })
        this.$notify.success({ title: '删除成功!' })
        this.getStoreList()
      }).catch(err => {
        this.$notify.error({ title: '删除异常！', message: err.message })
      })
    },
    addStore(storeShop) {
      return new Promise((resolve, reject) => {
        addData(storeShop).then(res => {
          resolve(res)
          const message = res.data.message
          if (!res.data.flag) {
            this.getStoreList()
            return this.$notify.error({ title: '新增失败!', message: message })
          }
          this.$notify.success({ title: '新增成功！' })
          this.getStoreList()
          this.dialogVisible = false
        }).catch(err => {
          this.$notify.error({ title: '新增异常！', message: err.message })
          reject(err)
        })
      })
    },
    updateStore(storeObj) {
      return new Promise((resolve, reject) => {
        updateData(storeObj).then(res => {
          resolve(res)
          const message = res.data.message
          if (!res.data.flag) {
            this.getStoreList()
            return this.$notify.error({ title: '保存失败!', message: message })
          }
          this.dialogVisible = false
        }).catch(err => {
          this.$notify.error({ title: '保存异常！', message: err.message })
          reject(err)
        })
      })
    },
    getStoreList(params = { data: this.searchData, pageInfo: this.pageInfo }) {
      this.setTableLoading('store', true)
      return new Promise((resolve, reject) => {
        listData(params).then(res => {
          this.setTableLoading('store', false)
          if (res.data.flag) {
            this.setTableData('store', res.data.data)
            this.pageInfo = res.data.pageInfo
          } else {
            this.$notify.error({ title: '查询失败！', message: res.data.message })
          }
          resolve(res)
        }).catch(err => {
          this.setTableLoading('store', false)
          this.$notify.error({ title: '查询异常！', message: err.message })
          reject(err)
        })
      })
    },
    initOptionsData() {
      getOptions().then(res => {
        if (res.data.flag) {
          const data = res.data.data
          this.$set(this.optionsData, 'shop', data)
        }
      })
    },
    // on
    onSelectionChange(selectionData) {
      this.setTableSelectionData('store', selectionData)
    },
    onSizeChange(val) {
      this.pageInfo.page = 1
      this.pageInfo.rows = val
      this.getStoreList()
    },
    onSwitchStatusChange(params, val) {
      const { row } = params
      this.updateStore(row).then(res => {
        if (res.data.flag) {
          this.$set(row, 'status', val)
        }
      })
    },
    onEditClosed(params, val) {
      const { row } = params
      this.updateStore(row).then(res => {
        if (res.data.flag) {
          // this.$set(row, 'status', val)
        }
      })
    },
    onCurrentChange(val) {
      this.pageInfo.page = val
      this.getStoreList()
    }
  }
}
</script>
<style scoped>

</style>
